package consultan.vanke.com.fragment;

import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.geek.banner.Banner;
import com.geek.banner.loader.BannerEntry;
import com.geek.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import consultan.vanke.com.R;
import consultan.vanke.com.adapter.CommonAdapter;
import consultan.vanke.com.adapter.ViewHolder;
import consultan.vanke.com.bean.BannerItem;
import consultan.vanke.com.utils.ToastUtils;
import qiu.niorgai.StatusBarCompat;

import static com.githang.statusbar.StatusBarTools.getStatusBarHeight;
import static consultan.vanke.com.utils.StatusBarUtil.setRootView;

public class HomePagerFrg extends BaseFragment {
    @BindView(R.id.edit_search)
    EditText editSearch;
    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.recy_center)
    RecyclerView recyCenter;
    @BindView(R.id.recy_bottom)
    RecyclerView recyBottom;
    @BindView(R.id.tv_dingfang)
    TextView tvDingfang;
    @BindView(R.id.tv_house)
    TextView tvHouse;
    @BindView(R.id.tv_shangcheng)
    TextView tvShangcheng;
    @BindView(R.id.tv_service)
    TextView tvService;
    @BindView(R.id.tv_house_sale)
    TextView tvHouseSale;
    @BindView(R.id.tv_food_sale)
    TextView tvFoodSale;
    private View inflate;
    private CommonAdapter<String> adapterCenter;
    private CommonAdapter<String> adapterBottom;
    private List<String> subjectsBeansCenter = new ArrayList<>();
    private List<String> subjectsBeansBottom = new ArrayList<>();

    @Override
    protected void initViews() {

    }

    @Override
    protected void initDatas() {
        setBanner();
        initCenter();
        initBottom();

    }

    private void initBottom() {
        subjectsBeansBottom.add("111");
        subjectsBeansBottom.add("111");
        subjectsBeansBottom.add("111");
        subjectsBeansBottom.add("111");
        recyBottom.setLayoutManager(new GridLayoutManager(_mActivity, 2));
        int space = 10;
        recyBottom.addItemDecoration(new SpacesItemDecoration(space));
        adapterBottom = new CommonAdapter<String>(_mActivity, subjectsBeansBottom, R.layout.home_item_food) {
            @Override
            public void onBindViewHolder(ViewHolder holder, int position) {
                super.onBindViewHolder(holder, position);
            }

            @Override
            public void convert(ViewHolder holder, String assetBean) {
//                ImageView img_photo = holder.getView(R.id.img_photo);

            }

        };
        recyBottom.setAdapter(adapterBottom);
    }

    private void initCenter() {
        subjectsBeansCenter.add("111");
        subjectsBeansCenter.add("111");
        subjectsBeansCenter.add("111");
        subjectsBeansCenter.add("111");
        LinearLayoutManager layoutManager = new LinearLayoutManager(_mActivity);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyCenter.setLayoutManager(layoutManager);
        adapterCenter = new CommonAdapter<String>(_mActivity, subjectsBeansCenter, R.layout.home_item_asset) {
            @Override
            public void onBindViewHolder(ViewHolder holder, int position) {
                super.onBindViewHolder(holder, position);
            }

            @Override
            public void convert(ViewHolder holder, String assetBean) {
//                ImageView img_photo = holder.getView(R.id.img_photo);

            }

        };
        recyCenter.setAdapter(adapterCenter);
    }

    private void setBanner() {
        // 1. 创建设置BannerLoader
        banner.setBannerLoader(new ImageLoader() {
            @Override
            public void loadView(Context context, BannerEntry entry, int position, View imageView) {
                Glide.with(context).load(entry.getBannerPath()).placeholder(R.mipmap.banner_default).apply(RequestOptions.bitmapTransform(new RoundedCorners(10))).into((ImageView) imageView);
            }
        });

        // 2 设置页面点击事件
        banner.setOnBannerClickListener(new Banner.OnBannerClickListener() {
            @Override
            public void onBannerClick(int position) {
                ToastUtils.show("点击了：" + position);
            }
        });

        // 3 翻页事件
        banner.setBannerPagerChangedListener(new Banner.OnBannerSimplePagerListener() {
            @Override
            public void onPageSelected(int position) {
                Log.d("hsl777", "onPageSelected: ==>" + position);
            }
        });

        // 4 最重要一步，加载数据
        List<BannerItem> bannerEntries = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            BannerItem bannerEntry = new BannerItem();
            bannerEntry.setPath(getResources().getDrawable(R.mipmap.banner_default));
            bannerEntry.setIndicatorText("" + i);
            bannerEntries.add(bannerEntry);
        }
        banner.loadImagePaths(bannerEntries);
    }

    @Override
    protected void initEvents() {
    }


    @Override
    protected View initContentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        inflate = View.inflate(_mActivity, R.layout.home_fragment, null);
        return inflate;
    }


    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        ViewGroup decorView = (ViewGroup) _mActivity.getWindow().getDecorView();
        View fakeStatusBarView = new View(_mActivity);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(_mActivity) + 3);
        fakeStatusBarView.setLayoutParams(params);
        fakeStatusBarView.setBackground(getResources().getDrawable(R.drawable.bg_white));
        fakeStatusBarView.setId(android.R.id.custom);
        decorView.addView(fakeStatusBarView);
        setRootView(_mActivity, false);
        StatusBarCompat.translucentStatusBar(_mActivity, false);
    }


    @Override
    public void onResume() {
        super.onResume();
        banner.startAutoPlay();
    }

    @Override
    public void onPause() {
        super.onPause();
        banner.stopAutoPlay();
    }

    @OnClick({R.id.tv_dingfang, R.id.tv_house, R.id.tv_shangcheng, R.id.tv_service, R.id.tv_house_sale, R.id.tv_food_sale})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_dingfang:
                startBrotherFragment(new DingfangFragment());
                break;
            case R.id.tv_house:
                startBrotherFragment(new HouseSettingsFragment());
                break;
            case R.id.tv_shangcheng:
                startBrotherFragment(ShangchengFrg.showis());
                break;
            case R.id.tv_service:
                startBrotherFragment(new MyServiceFragment());
                break;
            case R.id.tv_house_sale:
                startBrotherFragment(new FangchanFragment());
                break;
            case R.id.tv_food_sale:
                startBrotherFragment(new MeisFragment());
                break;
        }
    }

    public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
        private int space;

        public SpacesItemDecoration(int space) {
            this.space = space;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view,
                                   RecyclerView parent, RecyclerView.State state) {
            outRect.left = space;
            outRect.right = space;
            outRect.bottom = space;

            // Add top margin only for the first item to avoid double space between items
//            if (parent.getChildPosition(view) == 0)
            outRect.top = space;
        }
    }
}
